//
//  KeyboardWordsToolView.m
//  万物记
//
//  Created by Ajen on 2019/6/19.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "KeyboardWordsToolView.h"

@implementation KeyboardWordsToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *wordsBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [wordsBtn setTitle:@"词条管理" forState:UIControlStateNormal];
        wordsBtn.aj_buttonFont([UIFont systemFontOfSize:14 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [wordsBtn addTarget:self action:@selector(addwordsButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        [self addSubview:wordsBtn];
        [wordsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(20);
        }];
        
        UIView * cutLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        cutLine.backgroundColor = [UIColor colorWithAjenHex:0x7A7374 alpha:0.5];
        [self addSubview:cutLine];
        [cutLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.height.equalTo(@16);
            make.left.equalTo(wordsBtn.mas_right).offset(20);
            make.width.equalTo(@1);
        }];
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView.contentSize = CGSizeMake(1500, 35);

        [self addSubview:self.scrollView];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(wordsBtn.mas_right).offset(20);
            make.top.bottom.right.equalTo(self);
        }];
        self.scrollView.showsHorizontalScrollIndicator = FALSE;

        
        NSArray * titleArray = nil;
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"]){
            if([[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"] count]){
                titleArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"];
            }
        }
        
        [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"LOCAL_WORDS" options:NSKeyValueObservingOptionNew context:nil];
        
        
        
        for(int i = 0 ; i < titleArray.count ; i ++){
            UILabel * label = [UILabel new];
            NSString * string = titleArray[i];
            if(string.length > 10){
                string = [string substringFromIndex:8];
                string = [NSString stringWithFormat:@"%@...",string];
            }
            label.aj_Text([NSString stringWithFormat:@"(%d) : %@",i + 1,string]).aj_font([UIFont systemFontOfSize:14 weight:UIFontWeightMedium]).aj_textColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]).aj_addToView(self.scrollView);
            label.tag = 10000 + i;
            [label addTapBlock:^(id obj) {
                if(self.singleWordBlock){
                    self.singleWordBlock(titleArray[i]);
                }
            }];

            if(i == 0){
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.scrollView).offset(20);
                    make.bottom.top.equalTo(self);
                }];
            }else{
                UILabel * priorLabel = (UILabel *)[self viewWithTag:10000 + i - 1];//前一个label

                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(priorLabel.mas_right).offset(20);
                    make.bottom.top.equalTo(self);
                }];
            }
            
            if(i == titleArray.count - 1){
                [label.superview layoutIfNeeded];
                CGFloat lastLabelRight = label.Right;
                if(lastLabelRight < [UIScreen mainScreen].bounds.size.width - 20){
                    lastLabelRight = [UIScreen mainScreen].bounds.size.width - 20;
                }
                self.scrollView.contentSize = CGSizeMake(lastLabelRight + 20, self.scrollView.contentSize.height);
            }
        }
        
        
        
        UIView * topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        topLine.backgroundColor = [UIColor colorWithAjenHexString:@"#c5c5c5"];
        [self addSubview:topLine];
    }
    return self;
}

-(void)addwordsButtonDown{
    if(self.setWordsBlock){
        self.setWordsBlock();
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)contex{
    AJLog(@"%@",change);
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray * titleArray = nil;
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"]){
        if([[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"] count]){
            titleArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"];
        }
    }
    
    
    for(int i = 0 ; i < titleArray.count ; i ++){
        UILabel * label = [UILabel new];
        NSString * string = titleArray[i];
        if(string.length > 10){
            string = [string substringFromIndex:8];
            string = [NSString stringWithFormat:@"%@...",string];
        }
        label.aj_Text([NSString stringWithFormat:@"(%d) : %@",i + 1,string]).aj_font([UIFont systemFontOfSize:14 weight:UIFontWeightMedium]).aj_textColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]).aj_addToView(self.scrollView);
        label.tag = 10000 + i;
        [label addTapBlock:^(id obj) {
            if(self.singleWordBlock){
                self.singleWordBlock(titleArray[i]);
            }
        }];
        
        if(i == 0){
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.scrollView).offset(20);
                make.bottom.top.equalTo(self);
            }];
        }else{
            UILabel * priorLabel = (UILabel *)[self viewWithTag:10000 + i - 1];//前一个label
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(priorLabel.mas_right).offset(20);
                make.bottom.top.equalTo(self);
            }];
        }
        
        if(i == titleArray.count - 1){
            [label.superview layoutIfNeeded];
            CGFloat lastLabelRight = label.Right;
            if(lastLabelRight < [UIScreen mainScreen].bounds.size.width - 20){
                lastLabelRight = [UIScreen mainScreen].bounds.size.width - 20;
            }
            self.scrollView.contentSize = CGSizeMake(lastLabelRight + 20, self.scrollView.contentSize.height);
        }
    }
}


@end
